package cn.edu.ujn.project.supermarket.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.edu.ujn.project.common.annotation.Log;
import cn.edu.ujn.project.common.core.controller.BaseController;
import cn.edu.ujn.project.common.core.domain.AjaxResult;
import cn.edu.ujn.project.common.core.domain.entity.SysUser;
import cn.edu.ujn.project.common.core.page.TableDataInfo;
import cn.edu.ujn.project.common.enums.BusinessType;
import cn.edu.ujn.project.common.utils.poi.ExcelUtil;
import cn.edu.ujn.project.supermarket.domain.ChargeReport;
import cn.edu.ujn.project.supermarket.domain.SellReport;
import cn.edu.ujn.project.supermarket.domain.VipUser;
import cn.edu.ujn.project.supermarket.extendbean.SellReportChild;
import cn.edu.ujn.project.supermarket.extendbean.VipUserChild;
import cn.edu.ujn.project.supermarket.service.IChargeReportService;
import cn.edu.ujn.project.supermarket.service.IGoodsInforService;
import cn.edu.ujn.project.supermarket.service.ISellReportService;
import cn.edu.ujn.project.supermarket.service.IVipUserService;
import cn.edu.ujn.project.system.service.ISysUserService;
import tool.Generator;

/**
 * vip余额Controller
 * 
 * @author zhaomh
 * @date 2023-11-13
 */
@Controller
@RequestMapping("/system/vip")
public class VipUserController extends BaseController
{
    private String prefix = "system/vip";

    @Autowired
    private IVipUserService vipUserService;
    @Autowired
    private IChargeReportService chargeService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISellReportService sellReportService;
    @Autowired
    private IGoodsInforService goodsInforService;
    @RequiresPermissions("system:vip:view")
    @GetMapping()
    public String vip()
    {
        return prefix + "/vip";
    }

    /**
     * 查询vip余额列表
     */
    @RequiresPermissions("system:vip:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VipUser vipUser)
    {
        startPage();
        List<VipUserChild> vipUserChilds=new ArrayList<VipUserChild>();
        List<VipUser> list = vipUserService.selectVipUserList(vipUser);
        for (VipUser vipUser2 : list) {
			SysUser user = sysUserService.selectUserById(vipUser2.getUserId());
			vipUserChilds.add(new VipUserChild(user.getAvatar(), user.getUserName(), vipUser2));
		}
        return getDataTable(vipUserChilds);
    }

    /**
     * 导出vip余额列表
     */
    @RequiresPermissions("system:vip:export")
    @Log(title = "vip余额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VipUser vipUser)
    {
        List<VipUser> list = vipUserService.selectVipUserList(vipUser);
        ExcelUtil<VipUser> util = new ExcelUtil<VipUser>(VipUser.class);
        return util.exportExcel(list, "vip余额数据");
    }

    /**
     * 新增vip余额
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存vip余额
     */
    @RequiresPermissions("system:vip:add")
    @Log(title = "vip余额", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VipUser vipUser)
    {
    	String vipId=Generator.getString();
    	Long id= getSysUser().getUserId();
    	vipUser.setUserId(id);
    	vipUser.setVipId(vipId);
        chargeService.insertChargeReport(new ChargeReport(vipId,vipUser.getBalance()));
        return toAjax(vipUserService.insertVipUser(vipUser));
    }

    /**
     * 修改vip余额
     */
    @RequiresPermissions("system:vip:edit")
    @GetMapping("/edit/{vipId}")
    public String edit(@PathVariable("vipId") String vipId, ModelMap mmap)
    {
        VipUser vipUser = vipUserService.selectVipUserByVipId(vipId);
        
        mmap.put("vipUser", vipUser);
        return prefix + "/edit";
    }

    /**
     *  保存充值vip余额
     */
    @RequiresPermissions("system:vip:edit")
    @Log(title = "vip余额", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VipUser vipUser)
    {
    	System.out.println("充值");
        chargeService.insertChargeReport(new ChargeReport(Generator.getString(),vipUser.getVipId(),vipUser.getBalance()));
        return toAjax(vipUserService.vipUserCharge(vipUser));
    }

    /**
     * 删除vip余额
     */
    @RequiresPermissions("system:vip:remove")
    @Log(title = "vip余额", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vipUserService.deleteVipUserByVipIds(ids));
    }

    /**
     * 查询用户购买记录
     */
    @GetMapping("/purchaselist")
    public String purchaseListGet(ModelMap mmap){
        Long thisUserID = getUserId();
        mmap.put("currentBalance",this.vipUserService.selectVipUserByUserId(thisUserID).getBalance());
        return prefix + "/purchaselist";
    }

    @RequiresPermissions("system:vip:purchaselist")
    @PostMapping("/purchaselist")
    @ResponseBody
    public TableDataInfo purchaseList()
    {

        Long thisUserID = getUserId();
        String thisVipID = this.vipUserService.selectVipUserByUserId(thisUserID).getVipId();
        startPage();

//        SellReport sellReport = new SellReport();
//        sellReport.setVipId(thisVipID);
//        List<SellReport> sellReportList = this.sellReportService.selectSellReportList(sellReport);
//        List<SellReportChild> thisRecords = new ArrayList<>();
//        for(SellReport item:sellReportList){
//            SellReportChild temp = new SellReportChild();
//            temp.setVipId(thisVipID);
//            temp.setUserName(this.sysUserService.selectUserById(Long.valueOf(thisUserID)).getUserName());
//            temp.setBarCode(item.getBarCode());
//            temp.setBarCode(this.goodsInforService.selectGoodsInforByBarCode(item.getBarCode()).getBarCode());
//            temp.setName(this.goodsInforService.selectGoodsInforByBarCode(item.getBarCode()).getName());
//            temp.setQuantity(item.getQuantity());
//            temp.setDate(item.getDate());
//            thisRecords.add(temp);
//        }
//        return getDataTable(thisRecords);

        SellReportChild sellReportChild = new SellReportChild();
        sellReportChild.setVipId(thisVipID);
        List<SellReportChild> sellReportChildren = this.vipUserService.selectVipPurchaseRecordList(sellReportChild);
        System.out.println(sellReportChildren);
        return getDataTable(sellReportChildren);
    }

    @GetMapping("/chargevip")
    public String chargeThisVip( ModelMap mmap){
        System.out.println("ok----");
        mmap.put("vipUser",new VipUser());
        return prefix + "/chargeThisVip";
    }

    @PostMapping("/chargevip")
    @ResponseBody
    public void chargeThisVipPost(@RequestBody String json){
        double chargenum= JSON.parseObject(json,Double.class);

        Long thisUser = getUserId();
        String thisVipID = this.vipUserService.selectVipUserByUserId(thisUser).getVipId();
        // update ChargeReport
        ChargeReport chargeReport = new ChargeReport();
        chargeReport.setVipId(thisVipID);
        chargeReport.setChargeId(Generator.getString());
        chargeReport.setAmount(chargenum);
        this.chargeService.insertChargeReport(chargeReport);
        // update VipUser Table
        VipUser tempVipUser = new VipUser();
        tempVipUser.setVipId(thisVipID);
        tempVipUser.setUserId(thisUser);
        Double originalBalance = this.vipUserService.selectVipUserByUserId(thisUser).getBalance();
        tempVipUser.setBalance(originalBalance + chargenum);
        this.vipUserService.updateVipUser(tempVipUser);        
    }
}
